﻿using Exam.BLL;
using Exam.DBHelper;
using Exam.Model;
using ExaminationWeb.Common;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ExaminationWeb.Controllers
{
    public class LibraryController : Controller
    {
        //
        // GET: /Library/
        LibraryService service = new LibraryService();
        public ActionResult Index()
        {
            return View();
        }

        public JsonResult GetAllInfos()
        {
            int pageIndex = Request["sEcho"] == null ? 0 : int.Parse(Request["sEcho"]);
            int pageSize = Request["iDisplayLength"] == null ? 15 : int.Parse(Request["iDisplayLength"]);
            int pageStart = Request["iDisplayStart"] == null ? 0 : int.Parse(Request["iDisplayStart"]);

            string SortDirindex = Request["iSortCol_0"] == null ? "" : Request["iSortCol_0"];
            string SortDir = "";
            string DataProp = "";
            if (!string.IsNullOrEmpty(SortDirindex))
            {
                DataProp = Request["mDataProp_" + SortDirindex] == null ? "" : Request["mDataProp_" + SortDirindex];
                SortDir = Request["sSortDir_0"] == null ? "" : Request["sSortDir_0"]; //asc or desc
            }

            int totalcount = 0;
            string libraryname = Request["libraryname"] == null ? "" : Request["libraryname"].Trim();
            string isactive = Request["isactive"] == null ? "-1" : Request["isactive"];
            LibraryService libraryservice = new LibraryService();
            IQueryable<Library> liblist;

            if (isactive == "-1")
            {
                if (string.IsNullOrEmpty(libraryname))
                {
                    liblist = libraryservice.LoadEntites(e => true, pageStart, pageSize, out totalcount);
                }
                else
                {
                    liblist = libraryservice.LoadEntites(e => e.LibraryName.ToLower().Contains(libraryname.ToLower()), pageStart, pageSize, out totalcount);
                }
            }
            else
            {
                if (string.IsNullOrEmpty(libraryname))
                {
                    liblist = libraryservice.LoadEntites(e =>e.IsActive==int.Parse(isactive), pageStart, pageSize, out totalcount);
                }
                else
                {
                    liblist = libraryservice.LoadEntites(e =>e.IsActive==int.Parse(isactive)&& e.LibraryName.ToLower().Contains(libraryname.ToLower()), pageStart, pageSize, out totalcount);
                }
            }
            var results = new { iTotalDisplayRecords = totalcount, iTotalRecords = totalcount, aaData = liblist, sEcho = 0 };
            return Json(results, JsonRequestBehavior.AllowGet);
        }
        public ActionResult Add()
        {
            return View();
        }

        public string Addinfo(Library library)
        {
            library.IsActive = 0;
            //  library.CreateTime = DateTime.Now;
            library.LibraryID = Guid.NewGuid();
            library.CreateUserID = Guid.Parse("7d372642-aa1c-4874-af01-c589793bf31a");
            DateTime time = DateTime.Now;
            library.CreateTime = time;
            library.UpdateTime = time;

            service.AddEntity(library);

            return "suc";

        }
        public ActionResult Update(string id)
        {
            Guid guid;
            if (Guid.TryParse(id, out guid))
            {
                ViewData["Library"] = service.LoadEntites(e => e.LibraryID == guid).FirstOrDefault();
            }

            return View();
        }
        [HttpPost]
        public string UpdateInfo(Library library)
        {
            try
            {
                Library _pro = IsExistData(library);
                if (_pro.LibraryID.ToString() == "00000000-0000-0000-0000-000000000000" || _pro.LibraryID == library.LibraryID)
                {
                    library.UpdateTime = DateTime.Now;
                    library.UpdateUserID = UserAccount.UserID;
                    service.UpdateEntity(library);
                    return "suc";
                }
                else
                {
                    return "repeat";
                }
            }
            catch (Exception e)
            {
                return "err";
            }
        }
        public Library IsExistData(Library library)
        {
            SQLHelper commonservice = new SQLHelper();
            Dictionary<string, SqlParameter> dic = new Dictionary<string, SqlParameter>();

            SqlParameter parname = new SqlParameter("@LibraryName", System.Data.SqlDbType.NVarChar);
            parname.Value = library.LibraryName;
            dic.Add("LibraryName", parname);
            return commonservice.getExistData<Library>("Library", dic);
        }
    }
}
